<?php

/**
 *
 * @author Zsolt Lengyel
 *
 */
class ServicePriceController extends Controller
{


    /**
     *
     * @param integer $providerId
     * @param integer $blockId
     */
    public function actionIndex($providerId, $blockId)
    {

        $provider = $this->loadProvider($providerId);

        $this->accessDeniedIf(!checkAccess('ownProvider', array('provider'=>$provider)));

        $block = $this->loadBlock($blockId);

        $model=new ServicePrice('search');
        $model = $model->byProvider($provider);

        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ServicePrice'])){
            $model->attributes=$_GET['ServicePrice'];
        }

        $this->render('index',array(
                'model'=>$model,
                'provider'=>$provider,
                'block'=>$block
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($providerId, $blockId)
    {
        $provider = $this->loadProvider($providerId);
        $this->accessDeniedIf(!checkAccess('ownProvider', array('provider'=>$provider)));

        $block = $this->loadBlock($blockId);
        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));

        $servicePrice = new ServicePrice();

        $servicePrice->provider_id = $providerId;
        $servicePrice->price = 1;
        $servicePrice->fromDate = Yii::app()->dateFormatter->format('yyyy-MM-dd', time());

        if(isset($_POST['ServicePrice']))
        {
            $servicePrice->attributes=$_POST['ServicePrice'];

            $servicePrice->provider_id = $provider->id;

            if($servicePrice->save()){
                setFlash('success', 'Új ár létrehozva.');
                Yii::log($servicePrice->price.'hel3lo', 'debug');
                $this->redirect(array('index', 'providerId'=>$providerId, 'blockId'=>$blockId));
            }
        }

        $this->render('create',array('model'=>$servicePrice, 'provider'=>$provider, 'block'=>$block));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     * @param integer $providerId the ID of the provider
     */
    public function actionUpdate($id, $providerId, $blockId)
    {
        $provider = $this->loadProvider($providerId);

        // permission check
        $this->accessDeniedIf(!checkAccess('ownProvider', array('provider'=>$provider)));

        $servicePrice = $this->loadModel($id);

        if(isset($_POST['ServicePrice']))
        {
            $servicePrice->attributes=$_POST['ServicePrice'];

            $servicePrice->provider = $provider;

            if($servicePrice->save()){
                setFlash('success', 'Ár frissítve.');
                // $this->redirect(array('index', 'provierId'=>$providerId));
            }
        }


        $this->forward(array('index', 'provierId'=>$providerId, 'blockId'=>$blockId));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     * @param integer $blockId ID of block
     */
    public function actionDelete($id, $provierId, $blockId)
    {

        $servicePrice = $this->loadModel($id);

        $provider = $this->loadProvider($blockId);

        // permission check
        $this->accessDeniedIf(!checkAccess('ownProvider', array('provider'=>$provider)));


        $servicePrice->delete();


        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index', 'providerId'=>$provider->id, 'blockId'=>$blockId));
    }


    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    private function loadModel($id)
    {
        $model=ServicePrice::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    private function loadProvider($id)
    {
        $model=Provider::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    public function loadBlock($id)
    {
        $model=Block::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

}